package com.example.zsf.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.example.zsf.R;

public class TicketProgress extends RelativeLayout {
    private String title;
    private Double progress;
    private Context context;
    private Paint paint;

    public TicketProgress(Context context) {
        super(context);
        this.context = context;
        initUI();
    }

    public TicketProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initUI();
    }

    private void initUI() {
        setBackgroundColor(Color.parseColor("#6f000000"));

        paint = new Paint();
        paint.setColor(getResources().getColor(R.color.colorRed));
        paint.setStyle(Paint.Style.STROKE);
    }

    /**
     * 要不有title  要不有后面两个值
     * @param title 没有进度的时候 显示的标题
     * @param progress 进度
     * @param timeSpan 倒计时时间
     */
    public void setData(String title, Double progress, String timeSpan) {
        this.title = title;
        this.progress = progress;
        removeAllViews();
        if (this.title != null) {
            TextView textView = new TextView(this.context);
            textView.setText(title);
            textView.setTextColor(getResources().getColor(R.color.colorWhite));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
            LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(CENTER_IN_PARENT);
            textView.setLayoutParams(layoutParams);
            addView(textView);
        } else {
            TextView textView = new TextView(this.context);
            String content = timeSpan != null ? "剩余" + timeSpan : String.format("剩余%.0f%%", progress * 100);
            textView.setText(content);
            textView.setTextColor(getResources().getColor(R.color.colorWhite));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
            LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            layoutParams.setMarginEnd(ConvertUtils.dp2px(3));
            layoutParams.addRule(ALIGN_PARENT_END);
            layoutParams.addRule(CENTER_VERTICAL);
            textView.setLayoutParams(layoutParams);
            addView(textView);
        }
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if (this.title == null) {
            canvas.save();
            paint.setStrokeWidth(ConvertUtils.dp2px(7));
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setStrokeJoin(Paint.Join.ROUND);
            int lineWidth = getMeasuredWidth() - getChildAt(0).getMeasuredWidth() - ConvertUtils.dp2px(10);
            canvas.drawLine(0, ConvertUtils.dp2px(8), (float) (lineWidth * progress), ConvertUtils.dp2px(8), paint);
            canvas.restore();
        }
    }
}
